const Router = require('koa-router')
let router = new Router()
const mongoose = require('mongoose')

router.post('/insert', async (ctx) => {
    const OrderDetail = mongoose.model('OrderDetail')
    let newOrderDetail = new OrderDetail(ctx.request.body);
    await newOrderDetail.save().then(() => {
        ctx.body = {
            code: 200,
            message: "插入成功"
        }
    }).catch(error => {
        // 失败返回code=500，并返回错误信息
        ctx.body = {
            code: 500,
            message: error
        }
    })
})
// http://localhost:3000/orderDetail/insert

router.get('/getOrderDetail', async (ctx) => {
    let orderId = ctx.request.query.orderId
    let data = []
    const OrderDetail = mongoose.model('OrderDetail')
    let newOrderDetail = new OrderDetail(ctx.request.body);
    await OrderDetail.find({ order_id: orderId }).exec().then(async (result) => {
        if (result) {
            data = result
        }
        ctx.body = { code: 200, total: result.length, data: result }
    }).catch(error => {  // 报错抛出
        console.log(error)
        ctx.body = { code: 500, message: error }
    })
})
// http://localhost:3000/orderDetail/getOrderDetail

module.exports = router